#include "specparammodel.h"

SpecParamModel::SpecParamModel(SpecParamL *sp , QObject *parent) :
    QAbstractTableModel(parent)
{
    m_sp = sp;
}

int SpecParamModel::rowCount(const QModelIndex &parent) const
{
    return(m_sp->sizeOf());
}

int SpecParamModel::columnCount(const QModelIndex &parent) const
{
    return 6;
}

QVariant SpecParamModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid())
         return QVariant();

    if (index.row() < 0)
         return QVariant();

    if (index.row() >= m_sp->sizeOf())
         return QVariant();

    if (index.column() < 0)
         return QVariant();

        if (index.column() >= 6)
         return QVariant();

    SpecParam sp;
    sp = m_sp->getAt(index.row());
    if (role == Qt::DisplayRole)
    {
        QString qstr;
        switch (index.column())
        {
            case 0: // IDse
                return QVariant(qstr.fromStdString(sp.getKeyStr()));
            case 1: // agn
                return QVariant(sp.agn());
            case 2: // Fo
                return QVariant(sp.Fo());
            case 3: // Tsc
                return QVariant(sp.Tsc());
            case 4: // Descr
                return QVariant(qstr.fromStdString(sp.getDescr()));
            case 5: // IDtr
                {
                return QVariant(qstr.fromStdString(sp.getRefFrom(EXA_TAB_TEMPIRITORNO).getKeyStr()));
                }
        }
        return QVariant();
    }
    else if (role == Qt::UserRole)
    {
        return QVariant::fromValue(sp);
    }

    if (index.column() >= 0 && index.column() <=6 && role == Qt::TextAlignmentRole)
        return QVariant(Qt::AlignCenter|Qt::AlignVCenter);
    else
        return QVariant();
}

QVariant SpecParamModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (role != Qt::DisplayRole)
        return QVariant();

    if (orientation == Qt::Horizontal)
       switch (section)
       {
           case 0:
               return tr("IDse");
           case 1:
               return tr("agn");
           case 2:
               return tr("Fo");
           case 3:
               return tr("T*c");
           case 4:
               return tr("Descrizione");
           case 5:
               return tr("IDtr");
       }
    else
        return QString("Parametro sismico n.%1").arg(section+1);
}

bool SpecParamModel::insertRows(int position, int rows, const QModelIndex &parent )
{
    Q_UNUSED(parent);
    beginInsertRows(QModelIndex(), position, position+rows-1);
    m_sp->insertAt(position, rows);
    endInsertRows();
    return true;
}

bool SpecParamModel::removeRows(int position, int rows, const QModelIndex &parent )
{
    Q_UNUSED(parent);
    beginRemoveRows(QModelIndex(), position, position+rows-1);
    for (int row=0; row < rows; ++row) {
        m_sp->remove(position);
    }
    endRemoveRows();
    return true;
}

bool SpecParamModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (index.isValid() && role == Qt::EditRole)
    {
        int row = index.row();
        SpecParam sp = value.value< SpecParam >();
        m_sp->rename(row,sp.getKeyStr(),sp.getDescr());
        m_sp->valueAt(row) = sp;
        QModelIndex i = this->index(row,6);
        emit( dataChanged( index, i ) );
    }
    else return false;
    return true;
}

void SpecParamModel::updateWEDChange(void)
{
    reset();
    emit( dataChanged(QModelIndex(),QModelIndex()) );
}
